<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page import="entity.Admin" %>
        <%@ page import="entity.Owner" %>
            <%@ page import="dao.OwnerDao" %>
                <%@ page import="java.util.List" %>
                    <html>

                    <head>
                        <title>业主管理</title>
                        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
                            rel="stylesheet">
                        <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css"
                            rel="stylesheet">
                        <style>
                            body {
                                background-color: #f8f9fa;
                                padding-top: 2rem;
                            }

                            .card {
                                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                                border: none;
                            }
                        </style>
                    </head>

                    <body>
                        <% Admin admin=(Admin) session.getAttribute("admin"); if (admin==null) {
                            response.sendRedirect("../adminLogin.jsp"); return; } OwnerDao ownerDao=new OwnerDao();
                            List<Owner> owners = ownerDao.getAllOwners();
                            %>

                            <div class="container">
                                <div class="row justify-content-center">
                                    <div class="col-md-10">
                                        <div class="d-flex justify-content-between align-items-center mb-4">
                                            <div class="d-flex align-items-center">
                                                <a href="dashboard.jsp" class="btn btn-outline-secondary me-3">
                                                    <i class="bi bi-arrow-left"></i> 返回控制台
                                                </a>
                                                <h2>业主管理</h2>
                                            </div>
                                            <button type="button" class="btn btn-primary" data-bs-toggle="modal"
                                                data-bs-target="#addOwnerModal">
                                                <i class="bi bi-plus-lg"></i> 添加业主
                                            </button>
                                        </div>

                                        <div class="card">
                                            <div class="card-body">
                                                <div class="table-responsive">
                                                    <table class="table table-hover">
                                                        <thead class="table-light">
                                                            <tr>
                                                                <th>姓名</th>
                                                                <th>联系电话</th>
                                                                <th>房间号</th>
                                                                <th>入住时间</th>
                                                                <th>状态</th>
                                                                <th>操作</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <% for (Owner owner : owners) { %>
                                                                <tr>
                                                                    <td>
                                                                        <%= owner.getName() %>
                                                                    </td>
                                                                    <td>
                                                                        <%= owner.getPhone() %>
                                                                    </td>
                                                                    <td>
                                                                        <%= owner.getRoom() %>
                                                                            <!-- 使用 owner.getRoom() 显示房间号 -->
                                                                    </td>
                                                                    <td>
                                                                        <%= owner.getCreateTime() %> <!-- 显示入住时间 -->
                                                                    </td>

                                                                    <td>
                                                                        <%= owner.getType() !=null ?
                                                                            (owner.getType().equals("1") ? "业主" : "租户" )
                                                                            : "" %>
                                                                    </td>

                                                                    <td>
                                                                        <button class="btn btn-sm btn-outline-primary"
                                                                            onclick="viewOwner(<%= owner.getId() %>)">
                                                                            <i class="bi bi-eye"></i>
                                                                        </button>
                                                                        <button
                                                                            class="btn btn-sm btn-outline-secondary ms-1"
                                                                            onclick="editOwner(<%= owner.getId() %>)">
                                                                            <i class="bi bi-pencil"></i>
                                                                        </button>
                                                                        <button
                                                                            class="btn btn-sm btn-outline-danger ms-1"
                                                                            onclick="deleteOwner(<%= owner.getId() %>)">
                                                                            <i class="bi bi-trash"></i>
                                                                        </button>
                                                                    </td>
                                                                </tr>
                                                                <% } %>
                                                        </tbody>
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- 添加业主模态框 -->
                            <div class="modal fade" id="addOwnerModal" tabindex="-1">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title">添加业主</h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                        </div>
                                        <form action="${pageContext.request.contextPath}/admin/owner/add" method="post">
                                            <div class="modal-body">
                                                <div class="mb-3">
                                                    <label class="form-label">用户名</label>
                                                    <input type="text" class="form-control" name="username" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">密码</label>
                                                    <input type="password" class="form-control" name="password"
                                                        required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">姓名</label>
                                                    <input type="text" class="form-control" name="name" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">楼号</label>
                                                    <input type="text" class="form-control" name="building" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">单元</label>
                                                    <input type="text" class="form-control" name="unit" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">房间号</label>
                                                    <input type="text" class="form-control" name="roomNumber" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">类型</label>
                                                    <select class="form-select" name="type" required>
                                                        <option value="1">业主</option>
                                                        <option value="2">租户</option>
                                                    </select>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">车牌号</label>
                                                    <input type="text" class="form-control" name="plateNumber">
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">联系电话</label>
                                                    <input type="tel" class="form-control" name="phone" required>
                                                </div>
                                                <div class="mb-3">
                                                    <label class="form-label">入住时间</label>
                                                    <input type="date" class="form-control" name="checkInDate" required>
                                                </div>
                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-secondary"
                                                    data-bs-dismiss="modal">取消</button>
                                                <button type="submit" class="btn btn-primary">保存</button>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>

                            <script
                                src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
                            <script>
                                function viewOwner(id) {
                                    window.location.href = '${pageContext.request.contextPath}/admin/owner/view?id=' + id;
                                }

                                function editOwner(id) {
                                    window.location.href = '${pageContext.request.contextPath}/admin/owner/edit?id=' + id;
                                }

                                function deleteOwner(id) {
                                    if (confirm('确定要删除这个业主吗？此操作不可恢复！')) {
                                        window.location.href = '${pageContext.request.contextPath}/admin/owner/delete?id=' + id;
                                    }
                                }
                            </script>
                    </body>

                    </html>